<?php

class importTestsTask extends sfBaseTask
{
  protected function configure()
  {
    $this->namespace        = '';
    $this->name             = 'importTests';
    $this->briefDescription = 'Importa os testes do diretorio de testes configurado em settings.';
    $this->detailedDescription = <<<EOF
The [importTests|INFO] task does things.
Call it with:

  [php symfony importTests|INFO]
EOF;
    // add arguments here, like the following:
    //$this->addArgument('application', sfCommandArgument::REQUIRED, 'The application name');
    // add options here, like the following:
    //$this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev');
    $configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend' , 'dev', true);
    sfContext::createInstance($configuration);
    $databaseManager = new sfDatabaseManager($configuration);
    $databaseManager->loadConfiguration();
    //ELTestSuite::$databaseManager->getDatabase( SF_CONN );
  }

  protected function execute($arguments = array(), $options = array())
  {
    $dir = SettingTable::getSetting('testsDir');
    if(!file_exists($dir)) {
      throw new Exception('The directory ' . $dir . ', defined in settings, does not exists.\n');
    }
    $importCount = TestTable::importTests($dir);
    $msg = sprintf(
      'imported %s tests from %s',
      $importCount,
      $dir
    );
    echo $msg."\n";
    $dir = SettingTable::getSetting('testResultsFile');
    if(!file_exists($dir)) {
      throw new Exception('The directory ' . $dir . ', defined in settings, does not exists.\n');
    }
    $ret = TestTable::importTestResults($dir);
    $msg2 = sprintf(
        '%s tests updated, %s results not found.',
        $ret['testResultCount'],
        $ret['testCount'] - $ret['testResultCount']
    );
    echo $msg2."\n";
    DevTaskTable::sumTestsByTask();
    echo "calculated new task status\n";
  }
}